/**
 * 
 */
package pl.com.ozzy.ozsign.ui;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import pl.com.ozzy.ozsign.lang.NamedSignature;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/**
 *
 */
public class SignatureListAdapter extends ArrayAdapter<NamedSignature> {

	private Context mContext;
	private final LayoutInflater mInflater;
	private final Map<Long, Drawable> mThumbnails = Collections
			.synchronizedMap(new HashMap<Long, Drawable>());

	public SignatureListAdapter(Context context) {
		super(context, 0);
		mContext = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void addBitmap(Long id, Bitmap bitmap) {
		mThumbnails.put(id, new BitmapDrawable(bitmap));
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.signature_list_item, parent,
					false);
		}
		final NamedSignature signature = getItem(position);
		final TextView label = (TextView) convertView;
		label.setTag(signature);
		label.setText(signature.getName());
		label.setCompoundDrawablesWithIntrinsicBounds(
				mThumbnails.get(signature.getGesture().getID()), null, null, null);
		if (position == 2) {
			label.setTextAppearance(mContext, android.R.style.TextAppearance_Large);
		} else {
			label.setTextAppearance(mContext, android.R.style.TextAppearance);
		}
		return convertView;
	}
}
